// 获取全局应用程序实例对象
const app = getApp()

// 创建页面实例对象
Page({
    /**
     * 页面的初始数据
     */
    data: {
        title: 'orderPay',
        orderPayParams: {},
        findDateFront: "",
        findDateAfter: "",
        type: '',
        index: null,
        postj: {},
        items: [
            { name: 'CHN', value: '微信支付', checked: 'true' }
        ],
        array: [],
        clubId: "",
        totalSum: "",
        arrayNames: []
    },
    bindPickerChange: function(e) {
        this.data.orderPayParams.totalSum = Number(this.data.totalSum) - Number(this.data.array[e.detail.value].amount);
        this.data.orderPayParams.totalSum <= 0 ? this.data.orderPayParams.totalSum = 0.01 : this.data.orderPayParams.totalSum;
        this.data.orderPayParams.totalSum = getApp().util.returnFloat(this.data.orderPayParams.totalSum);
        this.setData({ orderPayParams: this.data.orderPayParams });
        this.setData({
            index: e.detail.value
        })
    },
    getOrderPayParams() {
        app.wechat.getStorage('orderPayParams')
            .then(res => {
                let findDateAfter = res.data.findDate.split("-");
                this.setData({ type: res.data.type });
                this.setData({ findDateAfter: findDateAfter[0] + "年" + findDateAfter[1] + "月" + findDateAfter[2] + "日" });
                this.setData({ orderPayParams: res.data });
                this.setData({ findDateFront: res.data.findDate });
                this.setData({ postj: res.data.postj });
                this.setData({ clubId: res.data.clubId });
                this.setData({ totalSum: this.data.orderPayParams.totalSum });
                this.getptCoupon();
            });
    },
    /**
     * 获取优惠票券
     */
    getptCoupon() {
        app.yds.getptCoupon(this.data.findDateFront, this.data.type, app.globalData.sessionId).then(res => {
            if (res.code && res.code == 0) {
                for (let i = 0; i < res.data.length; i++) {
                    this.data.arrayNames.push(res.data[i].projectName + '-' + res.data[i].amount + '元');
                }
                this.setData({ arrayNames: this.data.arrayNames });
                this.setData({ array: res.data });
            } else {
                wx.hideToast();
            }
        });
    },
    submitTicketOrder() {
        var j = null;
        if (this.data.index != null) {
            this.data.postj.couponNo = this.data.array[this.data.index].couponNo;
        }
        app.yds.submitTicketOrder(this.data.orderPayParams.clubId, this.data.postj, app.globalData.sessionId)
            .then(res => {
                if (res.code && res.code == 0) {
                    this.submitTicketOrderPay(res.data);
                } else {
                    // app.wechat.successToast("订单创建失败" + res.msg);
                    this.wetoast.toast({
                        duration: 2000,
                        title: res.msg,
                        img: '../../images/warn.png'
                    })
                }
            });
    },
    /**
     *准备支付订单
     */
    submitTicketOrderPay(orderCode) {
        let j = { "rlCardId": 0, "orderCode": orderCode };
        let that = this;
        app.yds.submitTicketOrderPay(this.data.orderPayParams.clubId, j, app.globalData.sessionId)
            .then(res => {
                if (res.code && res.code == 0) {
                    wx.requestPayment({
                        nonceStr: res.data.prepay.nonceStr,
                        package: res.data.prepay.package,
                        signType: res.data.prepay.signType,
                        timeStamp: res.data.prepay.timeStamp,
                        paySign: res.data.prepay.paySign,
                        success: function(res) {
                            app.wechat.successToast("支付成功,请在我的订单查看详情", 3000);
                            setTimeout(function(){
                                wx.switchTab({
                                    url: '../my/my'
                                })
                            }, 3000);
                        },
                        fail: function() {
                            that.wetoast.toast({
                                duration: 2000,
                                title: '支付失败',
                                img: '../../images/warn.png'
                            })
                        }
                        // ,
                        // complete: function(res) {
                        //     if (res.errMsg == "requestPayment:ok") {
                        //         app.wechat.successToast("支付成功!");
                        //         wx.successToast({
                        //             url: '../order/order'
                        //         })
                        //     }
                        // }
                    })
                } else {
                    wx.hideToast();
                }
            });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        // TODO: onLoad
        new app.WeToast();
        this.getOrderPayParams();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        // TODO: onReady
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        // TODO: onShow
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        // TODO: onHide
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        // TODO: onUnload
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        // TODO: onPullDownRefresh
    }
})
